import React, { Component } from 'react'
import './style.css'
export class TabControl extends Component {
  constructor(props) {
    super(props)
    this.state = {
      currentIndex: 0
    }
    
  }
  itemClick(index){
    this.setState({
      currentIndex: index
    })
    this.props.tabClick(index);
  }
  
  render() {
    const { titles, itemType } = this.props
    const { currentIndex } =this.state
    return (
      <div className="tab-control">
        {
          titles.map((item,index) => {
            return <div className={currentIndex === index ? "item active" : "item"} key={index} onClick={()=>this.itemClick(index)}> 
              {/* <span className="text">{item}</span> */}
              {/* 使用插槽方式  itemType 返回一个方法*/}
              {itemType(item)}
            </div>
          })
        }
      </div>
    )
  }
}

export default TabControl